/** 加载高德地图,并填充项目 */
define(['./../initial'], function () {
    var app = angular.module('App');
    app.factory('loadPublicMap', ['viewbag', function (viewbag) {
        //leaflet地图
        var map = null;
        //预定义的城市常量
        var predefinedCityInfoList = [
            {
                name: "上海",
                center: [31.1881336, 121.4737701],
                city: 'shanghai',
                hasLayer: true
            }, {
                name: "南京",
                center: [32.05720500, 118.77807441],
                city: 'nanjing',
                hasLayer: true
            }, {
                name: "慈溪",
                center: [30.337857356118082, 121.32169425487544],
                city: 'cixi',
                hasLayer: false
            }, {
                name: '杭州',
                city: 'hangzhou',
                center: [30.182406879513476, 120.26789724826837],
                hasLayer: true
            },
            {
                name: '长沙',
                city: 'changsha',
                center: [28.174457342377526, 113.1685727834704],
                hasLayer: false
            },
            {
                name: '青岛',
                city: 'qingdao',
                center: [36.13, 120.40],
                hasLayer: true
            }];

        /** 按照城市加载项目 */
        function loadPublicMap(city) {
            var c = predefinedCityInfoList.filter(function (item) {
                return item.name === city.name;
            })[0];
            if (map) {
                map.remove();
            }
            map = L.map('map', {
                center: c.center,
                zoom: 13,
                maxZoom: 18,
                minZoom: 8,
                zoomControl: false,
                doubleClickZoom: false,
                closePopupOnClick: false
            });
            var gaodeMapLayers = L.layerGroup([L.tileLayer('http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}', {
                attribution: '&copy; <a target="_blank" href="http://www.amap.com/">高德地图</a>',
                detectRetina: true,
                reuseTiles: true
            })]);
            city.lines.forEach(function (line) {
                line.projects.forEach(function (prj) {
                    var marker = L.marker([prj.center.lat, prj.center.lng], {
                        icon: L.AwesomeMarkers.icon({
                            icon: 'hh',
                            prefix: 'i',
                            markerColor: viewbag.curType.markerColor,
                            iconColor: '#fff',
                            extraClasses: 'fa fa-home'
                        })
                    });
                    map.addLayer(marker);
                });
            });
            map.addLayer(gaodeMapLayers);
        }

        return loadPublicMap;
    }]);
});